package io.gitee.chearnee.fw.common.model.vo;

import io.gitee.chearnee.fw.common.model.IResultCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
public class ResultVo<T> implements IResultCode {
    private int code;
    private boolean success;
    private String message;
    private T data;

    private ResultVo(IResultCode resultCode, T data, String message) {
        this.code = resultCode.getCode();
        this.success = resultCode.getCode() == ResultCode.SUCCESS.getCode();
        this.message = message;
        this.data = data;
    }

    private ResultVo(IResultCode resultCode, T data) {
        this(resultCode, data, resultCode.getMessage());
    }

    public static <T> ResultVo<T> success(T data) {
        return new ResultVo<>(ResultCode.SUCCESS, data);
    }

    public static <T> ResultVo<T> fail(String message) {
        return fail(ResultCode.FAILED, message);
    }

    public static <T> ResultVo<T> fail(IResultCode resultCode) {
        return fail(resultCode, resultCode.getMessage());
    }

    public static <T> ResultVo<T> fail(IResultCode resultCode, String message) {
        return new ResultVo<>(resultCode, null, message);
    }
}
